/*
 * DownloadView.java
 *
 * Created on __DATE__, __TIME__
 */

package pl.streamsoft.test.currency_view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JOptionPane;

import net.sourceforge.jdatepicker.JDateComponentFactory;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import pl.streamsoft.test.currency_buisness.CurrenciesService;
import pl.streamsoft.test.currency_buisness.exceptions.ServiceException;
import pl.streamsoft.test.currency_view.helpers.DateHelper;
import pl.streamsoft.test.currency_view.storage.Storage;

/**
 * 
 * @author __USER__
 */
public class DownloadView extends javax.swing.JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8132816217040658164L;

	/** Creates new form DownloadView */
	public DownloadView(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		fromDateLabel = new javax.swing.JLabel();
		downloadButton = new javax.swing.JButton();
		jProgressBar1 = new javax.swing.JProgressBar();
		fromDateDatePicker = (JDatePickerImpl) JDateComponentFactory
				.createJDatePicker();
		fromDateDatePicker.getModel().setSelected(true);
		toDateLabel = new javax.swing.JLabel();
		toDateDatePicker = (JDatePickerImpl) JDateComponentFactory
				.createJDatePicker();
		toDateDatePicker.getModel().setSelected(true);
		currencyAcronymTextField = new javax.swing.JTextField();
		currencyAcronymLabel = new javax.swing.JLabel();
		closeButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		fromDateLabel.setText("Od");
		downloadButton.setText("Pobierz");
		toDateLabel.setText("Do");
		currencyAcronymLabel.setText("Waluta");

		closeButton.setText("Zamknij");
		closeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				closeButtonActionPerformed(e);
			}
		});

		downloadButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				downloadButtonActionPerformed(e);

			}

		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jProgressBar1,
														javax.swing.GroupLayout.Alignment.TRAILING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														376, Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						fromDateDatePicker,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						140,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						fromDateLabel))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						toDateDatePicker,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						140,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						toDateLabel))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		69,
																		Short.MAX_VALUE)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						currencyAcronymLabel)
																				.addComponent(
																						currencyAcronymTextField,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						100,
																						javax.swing.GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addComponent(
																		downloadButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		closeButton)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(fromDateLabel)
												.addComponent(toDateLabel)
												.addComponent(
														currencyAcronymLabel))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														fromDateDatePicker,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														toDateDatePicker,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														currencyAcronymTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										183, Short.MAX_VALUE)
								.addComponent(jProgressBar1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(closeButton)
												.addComponent(downloadButton))
								.addContainerGap()));

		pack();
	}// </editor-fold>
		// GEN-END:initComponents

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
	}

	private void asyncJob() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				CurrenciesService controller = new CurrenciesService();
				try {
					Date fromDate = DateHelper.getDate(fromDateDatePicker);
					Date toDate = DateHelper.getDate(toDateDatePicker);
					String currencyAcronym = currencyAcronymTextField.getText();
					controller.downloadCurrencies(fromDate, toDate,
							currencyAcronym);
					Storage.getInstance().getCurrencyTableModel()
							.reloadFromServer();
				} catch (ServiceException e) {
					e.printStackTrace();
				}
				// TODO download
				jProgressBar1.setIndeterminate(false);
				jProgressBar1.setValue(jProgressBar1.getMaximum());
			}
		}).start();
	}

	public void startDownload() {

		jProgressBar1.setIndeterminate(true);
		asyncJob();

	}

	private void downloadButtonActionPerformed(ActionEvent e) {
		if (readyToDownload()) {
			startDownload();
		} else {
			JOptionPane.showMessageDialog(this, "Wszystkie pola sa wymagane",
					"Pobieranie", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	public boolean readyToDownload() {
		if (!fromDateDatePicker.getModel().isSelected()) {
			return false;
		}
		if (!toDateDatePicker.getModel().isSelected()) {
			return false;
		}
		if (currencyAcronymTextField.getText().isEmpty()) {
			return false;
		}

		return true;
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				DownloadView dialog = new DownloadView(
						new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton downloadButton;
	private javax.swing.JButton closeButton;
	private javax.swing.JLabel fromDateLabel;
	private javax.swing.JLabel toDateLabel;
	private javax.swing.JLabel currencyAcronymLabel;
	private javax.swing.JProgressBar jProgressBar1;
	private JDatePickerImpl fromDateDatePicker;
	private JDatePickerImpl toDateDatePicker;
	private javax.swing.JTextField currencyAcronymTextField;
	// End of variables declaration//GEN-END:variables

}